package cn.iocoder.an.module.mold.service.repairfaultcategory;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.an.module.mold.dal.dataobject.account.AccountDO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.an.module.mold.controller.admin.repairfaultcategory.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.repairfaultcategory.RepairFaultCategoryDO;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import cn.iocoder.an.module.mold.dal.mysql.repairfaultcategory.RepairFaultCategoryMapper;

import static cn.iocoder.an.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.an.module.mold.enums.ErrorCodeConstants.*;

/**
 * 模具维修故障分类 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class RepairFaultCategoryServiceImpl implements RepairFaultCategoryService {

    @Resource
    private RepairFaultCategoryMapper repairFaultCategoryMapper;

    @Override
    public Long createRepairFaultCategory(RepairFaultCategorySaveReqVO createReqVO) {
        // 插入
        RepairFaultCategoryDO repairFaultCategory = BeanUtils.toBean(createReqVO, RepairFaultCategoryDO.class);
        repairFaultCategoryMapper.insert(repairFaultCategory);

        // 返回
        return repairFaultCategory.getId();
    }

    @Override
    public void updateRepairFaultCategory(RepairFaultCategorySaveReqVO updateReqVO) {
        // 校验存在
        validateRepairFaultCategoryExists(updateReqVO.getId());
        // 更新
        RepairFaultCategoryDO updateObj = BeanUtils.toBean(updateReqVO, RepairFaultCategoryDO.class);
        repairFaultCategoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteRepairFaultCategory(Long id) {
        // 校验存在
        validateRepairFaultCategoryExists(id);
        // 删除
        repairFaultCategoryMapper.deleteById(id);
    }

    @Override
        public void deleteRepairFaultCategoryListByIds(List<Long> ids) {
        // 删除
        repairFaultCategoryMapper.deleteByIds(ids);
        }


    private void validateRepairFaultCategoryExists(Long id) {
        if (repairFaultCategoryMapper.selectById(id) == null) {
            throw exception(REPAIR_FAULT_CATEGORY_NOT_EXISTS);
        }
    }

    @Override
    public RepairFaultCategoryDO getRepairFaultCategory(Long id) {
        return repairFaultCategoryMapper.selectById(id);
    }

    @Override
    public PageResult<RepairFaultCategoryDO> getRepairFaultCategoryPage(RepairFaultCategoryPageReqVO pageReqVO) {
        return repairFaultCategoryMapper.selectPage(pageReqVO);
    }


    @Override
    public  List<RepairFaultCategoryDO> getFaultCatAll(){
        return repairFaultCategoryMapper.selectList();
    };
}